package com.jcsz.gulimall.order.interceptor;

import com.jcsz.common.constant.AuthServerConstant;
import com.jcsz.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @className: LoginInterceptor
 * @author: 一把Po吉他
 * @date: 2022/11/12 11:38
 * @version: 1.0
 **/
@Component
public class LoginInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberRespVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object attribute = request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        //从库存服务进来直接放行
        String requestURI = request.getRequestURI();
        boolean match = new AntPathMatcher().match("/order/order/statusInfo/**", requestURI);
        boolean pay = new AntPathMatcher().match("/payed/**", requestURI);
        if (match||pay) {
            return true;
        }
        if (attribute != null) {
            threadLocal.set((MemberRespVo) attribute);
            return true;
        } else {
            request.getSession().setAttribute("msg", "登录后查看订单.....");
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}
